//传入年份和月份 获取该年对应月份的天数
export function getMonthDays(year,month){
  let thisDate
  //当天数为0 js自动处理为上一月的最后一天
  if (month < 12) {
    thisDate = new Date(year, month, 0);
  } else {
    thisDate = new Date(year + 1, 1, 0);
  }
  return thisDate.getDate();
}
// 日期范围限制及图表x轴数据生成
export function dateChange(date) {
  const startDate = date[0]
  const endDate = date[1]
  const startYear = startDate.getFullYear()
  const startMonth = startDate.getMonth() + 1
  const startDay = startDate.getDate()
  const endYear = endDate.getFullYear()
  const endMonth = endDate.getMonth() + 1
  const endDay = endDate.getDate()
  if (endDate.valueOf() - startDate.valueOf() > 60 * 24 * 60 * 60 * 1000) {
    this.$message.warning('时间范围超过了60天，请重新选择！')
    this.listQuery.datetime = null
    return
  } else if (endYear - startYear > 0){
    this.$message.warning('时间范围跨年，请重新选择！')
    this.listQuery.datetime = null
    return
  }

  // x轴数据生成
  const xAxisData = []
  if (startMonth === endMonth) {
    for (let i = 0; i <= endDay - startDay; i++) {
      const item = startMonth + '月' + (startDay + i) + '日'
      xAxisData.push(item)
    }
  } else {
    // 首月
    const startDays = getMonthDays(startYear, startMonth)
    for (let i = 0; i <= startDays - startDay; i++) {
      const item = startMonth + '月' + (startDay + i) + '日'
      xAxisData.push(item)
    }
    // 中间月
    for (let i = 1; i < endMonth - startMonth; i++) {
      const monthDays = getMonthDays(startYear, startMonth + i)
      for (let j = 1; j <= monthDays; j++) {
        const item = (startMonth + i) + '月' + j + '日'
        xAxisData.push(item)
      }
    }
    // 尾月
    const endDays = getMonthDays(endYear, endMonth)
    for (let i = 1; i <= endDay; i++) {
      const item = endMonth + '月' + i + '日'
      xAxisData.push(item)
    }
  }
  
}

